import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:list_test/weight.dart';
import 'package:list_test/dialog.dart';
import 'package:list_test/store/redux_core.dart';
import 'package:list_test/store/actions.dart';
import 'package:list_test/pages/history_page.dart';
import 'package:list_test/pages/statistics_page.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);

  final String title;

  _HomePageState createState () => new _HomePageState();
}

@immutable
class HomePageViewModel {
  final List<Weight> entries;
  final Function(Weight) addEntryCallback;
  final Function() acceptEntryAddedCallback;
  final bool hasEntryBeenAdded;

  HomePageViewModel({
    this.entries,
    this.addEntryCallback,
    this.hasEntryBeenAdded,
    this.acceptEntryAddedCallback
  });
}


class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 2);
  }

  @override
  void dispose() {
    _tabController.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  Future _openDialog(List<Weight> entries, Function(Weight) onSubmittedCallback) async {
    Weight result = await Navigator.of(context).push(new MaterialPageRoute<Weight>(
      builder: (BuildContext build) {
        return new AddEntryDialog.add(
          entries.isNotEmpty ? entries.last.weight : 50.0
        );
      },
      fullscreenDialog: true,
    ));
    if (result != null) {
      onSubmittedCallback(result);
    }
  }

  @override
  Widget build(BuildContext context) {
    return new StoreConnector<ReduxState, HomePageViewModel>(
      converter: (store) {
        return new HomePageViewModel(
          entries: store.state.entries,
          addEntryCallback: ((entry) => store.dispatch(new AddEntryAction(entry))),
          hasEntryBeenAdded: store.state.hasEntryBeenAdded,
          acceptEntryAddedCallback: () => store.dispatch(new AcceptEntryAddedAction())
        );
      },
      builder: (context, viewModel) {
        if (viewModel.hasEntryBeenAdded) {
          viewModel.acceptEntryAddedCallback();
        }
        return new Scaffold(
          appBar: new AppBar(
            title: new Text(widget.title),
            bottom: new TabBar(
              tabs: <Tab>[
                new Tab(
                  text: 'STATISTICS',
                  icon: new Icon(Icons.show_chart),
                ),
                new Tab(
                  text: 'HISTORY',
                  icon: Icon(Icons.history)
                )
              ],
              controller: _tabController,
            ),
          ),
          body: new TabBarView(
            children: <Widget>[
              new StatisticsPage(),
              new HistoryPage()
            ],
            controller: _tabController,
          ),
          floatingActionButton: new FloatingActionButton(
            onPressed: (() => _openDialog(viewModel.entries, viewModel.addEntryCallback)),
            tooltip: 'Add',
            child: Icon(Icons.add),
          )
        );
      },
    );
  }
}